package com.bdk.middle.tenant.web.orchestration;


import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.orchestration.api.client.AppClientApi;
import com.bdk.middle.orchestration.api.domain.AppManagerDO;
import com.bdk.middle.orchestration.api.domain.FunctionModuleDO;
import com.bdk.middle.orchestration.api.query.AppManagerQueryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/app-manager")
public class AppManagerController {

    @Autowired
    private AppClientImpl appClientImpl;

    @FeignClient(value= "orchestration-service", contextId="app", path = "/app")
    interface AppClientImpl extends AppClientApi {

    }

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody AppManagerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = appClientImpl.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody AppManagerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = appClientImpl.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody AppManagerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = appClientImpl.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody AppManagerDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = appClientImpl.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/status.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse status(@RequestBody FunctionModuleDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = appClientImpl.updateStatus(domain.getId(), domain.getStatus(), "");

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody AppManagerQueryDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = appClientImpl.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/set_module.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse setModule(@RequestBody AppManagerDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = appClientImpl.setAppModule(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

}

